package com.ruoyi.web.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.juhang.domain.Apply;
import com.ruoyi.juhang.domain.Post;
import com.ruoyi.juhang.service.IApplyService;
import com.ruoyi.juhang.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/app/user")
public class AppUserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IApplyService applyService;

    /**
     * 职位报名
     * @param apply
     * @return
     */
    @PostMapping("/applyPost")
    public AjaxResult applyPost(@RequestBody Apply apply){
        List<Apply> list = applyService.list(new QueryWrapper<Apply>().eq("user_id", apply.getUserId()).eq("post_id", apply.getPostId()));
        if(list.size() == 0){
            apply.setCreateTime(new Date());
            return AjaxResult.success("报名成功",applyService.save(apply));
        }else {
            return AjaxResult.error("您已报过名了");
        }
    }

    /**
     * 获取我报名的职位
     * @param userId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getApplyPostList")
    public AjaxResult getApplyPostList(Integer userId,Integer pageNo,Integer pageSize){
        Page<Post> page = new Page<>(pageNo,pageSize);
        IPage<Post> postIPage = applyService.getApplyPostList(page, new QueryWrapper<Post>().eq("a.user_id", userId).eq("p.state",0).orderByDesc("a.id"));
        return AjaxResult.success(postIPage);
    }
}
